package com.ytjj.qmyx.data.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ytjj.common.constants.RedisKeyConstant;
import com.ytjj.common.dto.mqdto.ProductStatisticsDto;
import com.ytjj.common.service.RedisService;
import com.ytjj.qmyx.data.constants.MongoCollectionNameConstants;
import com.ytjj.qmyx.data.model.mongo.ProductStatistics;
//import com.ytjj.qmyx.data.service.KafkaSendService;
import com.ytjj.qmyx.data.service.ProductStatisticsService;
import com.ytjj.qmyx.data.util.CalendarUtil;
import com.ytjj.qmyx.mall.mapper.ProductMapper;
import com.ytjj.qmyx.mall.model.Product;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 *
 *  *
 **/
@Slf4j
@Service
public class ProductStatisticsServiceImpl implements ProductStatisticsService {

    @Resource
    private MongoTemplate mongoTemplate;

//    @Autowired
//    private KafkaSendService kafkaSendService;

    @Autowired
    private RedisService redisService;

    @Resource
    private ProductMapper productMapper;

    @Override
    public void productStatistics(ProductStatisticsDto productStatisticsDto) {
        ProductStatistics productStatistics = new ProductStatistics();
        BeanUtils.copyProperties(productStatisticsDto, productStatistics);
        String dayTime = CalendarUtil.dateFormat(productStatisticsDto.getCreateTime());
        Integer hourTime = Integer.parseInt(CalendarUtil.dateFormatHour(productStatisticsDto.getCreateTime()));
        productStatistics.setDayTime(dayTime);
        productStatistics.setHourTime(hourTime);
        if (StringUtils.isEmpty(productStatistics.getSeq_id())) {
            productStatistics.setSeq_id(UUID.randomUUID().toString().replace("-", ""));
        }
        if (StringUtils.isEmpty(productStatistics.getCur_date())) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            productStatistics.setCur_date(sdf.format(new Date()));
        }
        Product product=productMapper.selectByPrimaryKey(productStatistics.getProductId());
        if(null!=product){
            productStatistics.setActivityColumnIds(product.getActivityColumnIds());
        }
        mongoTemplate.save(productStatistics);

        mongoTemplate.save(productStatistics, MongoCollectionNameConstants.PRODUCT+dayTime);

        //获取自增主键
        Long incrementId = redisService.getIncrementId(RedisKeyConstant.PRODUCT_KAFKA_OUT_KEY);
        productStatistics.setIncrementId(incrementId);
        productStatistics.setCur_timestamp(System.currentTimeMillis());

        //发送到kafka
//        try{
//            log.debug("productStatistics={}", JSONObject.toJSONString(productStatistics));
//            boolean sendStatus=kafkaSendService.sendProductMsg(JSONObject.toJSONString(productStatistics));
//            log.debug("sendProductStatus={}",true);
//        }catch (Exception e){
//            log.error("发送到kafka错误",e);
//        }
    }

}
